<?php
/*
 * @Author: cwj
 * @Date: 2019-12-20 09:54:03
 * @LastEditTime: 2020-12-28 09:55:43
 * @LastEditors: Please set LastEditors
 * @Description: 命令下发 历史 
 * @FilePath: /ahsmp_3/application/adminapiv3/service/ClientCmdsHistory.php
 */
namespace app\adminapiv3\service\platform_manage\log;

use app\adminapiv3\service\BaseService;

use app\common\service;
use think\Db;

class CmdsHistoryService extends BaseService
{
    /**
     * @name: 列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 09:55:15
     */
    public function list ()
    {
        $ServerSocketMessageHistory = new service\platform_manage\log\ServerSocketMessageHistoryService();
        //根据当前登录用户所属资产组查和用户是否为超级管理员询
        $return = $ServerSocketMessageHistory->list($this->getLoginHostGroupId(), $this->isRootRoleGroup());
        $return['data'] = $this->processList($return['data']);
        return $return;
    }

    /**
     * @name: 处理列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-20 11:19:24
     */
    public function processList ($data)
    {
        if(empty($data))
        {
            return [];
        }
        
        foreach ($data as $v)
        {
            $host_id_a[] = $v['host_id'];
            $user_id_a[] = $v['user_id'];
        }

        $ServerUser = new service\platform_manage\account\ServerUserService();
        $user_a = $ServerUser->getUserById($user_id_a);

        $Host = new service\Host();
        $host_a = $Host->hostList($host_id_a, 'id,name,ctime');

        foreach ($data as $k => $v)
        {
            $data[$k]['host'] = isset($host_a[$v['host_id']]) ? $host_a[$v['host_id']] : ['name'=>'该主机已被删除'];
            $data[$k]['user'] = isset($user_a[$v['user_id']]) ? $user_a[$v['user_id']] : ($v['user_id'] == 0 ? ['show_name'=> '系统下发'] : ['show_name'=> '该用户已被删除']);
        }
        return $data;
    }

    /**
     * @name: 发送失败条数
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-03 14:07:45
     */
    public function errorCount ()
    {
        $userId = $this->getLoginUserId();
        $ServerSocketMessageHistory = new service\platform_manage\log\ServerSocketMessageHistoryService();
        return $ServerSocketMessageHistory->errCount($userId);
    }

    /**
     * @name: 忽略
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-03 14:24:33
     */
    public function ignore ($id_s)
    {
        $ServerSocketMessageHistory = new service\platform_manage\log\ServerSocketMessageHistoryService();
        return $ServerSocketMessageHistory->ignore($id_s);
    }
}
